package com.example.demo.application.myredis;

import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * <一句话功能简述> <功能详细描述>
 * 
 * @author 87431
 * @version [版本号，2018年11月11日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class RedisProvider {
	protected static final Logger LOG = LoggerFactory.getLogger(RedisProvider.class);
	// single redis
	protected static JedisPool jedispool;
	protected static JedisCluster jedisCluster;

	protected static int EXPIRE = 130;
	private static AtomicBoolean isLoad = new AtomicBoolean(true);

	/**
	 * 重加载 redis (失去连接后，或更改配置后)，当出现异常时触发。 <一句话功能简述> <功能详细描述>
	 * 
	 * @see [类、类#方法、类#成员]
	 */
	public static void reload() {
		if (isLoad.get()) {
			isLoad.set(false);
			ReloadConfig.reload();
			jedisCluster = ReloadConfig.getCluster();
			jedispool = ReloadConfig.getJedisPool();
			EXPIRE = ReloadConfig.defalutExpire;
			isLoad.set(true);
		}
	}

	public static JedisCluster getJedisCluster() {
		if (null == jedisCluster) {
			jedisCluster = ReloadConfig.getCluster();
		}
		return jedisCluster;
	}
	
	public static Jedis getJedis() {
		if (null == jedispool) {
			jedispool = ReloadConfig.getJedisPool();
		}
        return jedispool.getResource();
	}

}
